//
//  PopoverPresentationController.swift
//  Weibo
//
//  Created by zxy on 2018/12/3.
//  Copyright © 2018 zxy. All rights reserved.
//

import UIKit

class PopoverPresentationController: UIPresentationController {
    //定义属性保存popover的frame
    var popovetFrame: CGRect = CGRect.zero
    /// 重写父类方法-用于创建负责转场动画的对象
    ///
    /// - Parameters:
    ///   - presentedViewController: 要展示的控制器
    ///   - presentingViewController: 发起的控制器
    override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
        super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
      
    }
    //专门负责设置容器view的frame
    override func containerViewWillLayoutSubviews(){
        super.containerViewWillLayoutSubviews()
        //presentedView:要展示控制器view
        //containerView:容器视图View,在presentedView下面
        if popovetFrame == CGRect.zero {//没有设置给个默认frame
             presentedView?.frame = CGRect(x: 100, y: 56, width: 200, height: 200)
        }else{
            presentedView?.frame = popovetFrame
        }
        
        //给容器视图添加蒙蔽
        containerView?.insertSubview(maskView, at: 0)
    }
    
    //懒加载遮罩View
    private lazy var maskView: UIView = {
        let v = UIView(frame: UIScreen.main.bounds)
        v.backgroundColor = UIColor(white: 0, alpha: 0.3)
        //添加蒙蔽手势
        let tap = UITapGestureRecognizer(target: self, action: #selector(dimissController))
        v.addGestureRecognizer(tap)
        return v
    }()
    
    @objc private func dimissController(){
        presentingViewController.dismiss(animated: true, completion: nil)
    }
}
